package httpclient.bean;

import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang.StringUtils;

/**
 * Created by yangqy on 2017/2/15.
 */
public final class HttpResult<T> {
    private Response response;
    private ResponseBody responseBody;
    private T result;
    private String bodyStr;
    private String rawBodyStr;
    private String error;

    private HttpResult(Response response, ResponseBody responseBody, T result, String bodyStr, String rawBodyStr, String error) {
        this.response = response;
        this.responseBody = responseBody;
        this.result = result;
        this.bodyStr = bodyStr;
        this.rawBodyStr = rawBodyStr;
        this.error = error;
    }

    public boolean isHttpSuccessful() {
        return this.response == null?false:this.response.isSuccessful();
    }

    public boolean isSuccessful() {
        return this.isHttpSuccessful()? StringUtils.isBlank(this.error):false;
    }

    public Response getResponse() {
        return this.response;
    }

    public T getResult() {
        return this.result;
    }

    public String getError() {
        return this.error;
    }

    public ResponseBody getResponseBody() {
        return this.responseBody;
    }

    public String getBodyStr() {
        return this.bodyStr;
    }

    public String getRawBodyStr() {
        return this.rawBodyStr;
    }

    public static final class Builder<T> {
        private Response response;
        private ResponseBody responseBody;
        private T result;
        private String bodyStr;
        private String rawBodyStr;
        private String error;

        public Builder() {
        }

        public HttpResult<T> build() {
            return new HttpResult(this.response, this.responseBody, this.result, this.bodyStr, this.rawBodyStr, this.error);
        }

        public Builder<T> setResponse(Response response) {
            this.response = response;
            return this;
        }

        public Builder<T> setResponseBody(ResponseBody responseBody) {
            this.responseBody = responseBody;
            return this;
        }

        public Builder<T> setResult(T result) {
            this.result = result;
            return this;
        }

        public Builder<T> setBodyStr(String bodyStr) {
            this.bodyStr = bodyStr;
            return this;
        }

        public Builder<T> setRawBodyStr(String rawBodyStr) {
            this.rawBodyStr = rawBodyStr;
            return this;
        }

        public Builder<T> setError(String error) {
            this.error = error;
            return this;
        }

        public Response getResponse() {
            return response;
        }

        public ResponseBody getResponseBody() {
            return responseBody;
        }

        public T getResult() {
            return result;
        }

        public String getBodyStr() {
            return bodyStr;
        }

        public String getRawBodyStr() {
            return rawBodyStr;
        }

        public String getError() {
            return error;
        }
    }
}

